package com.dhnsoft.数据结构与算法.哈希表;

import sun.net.www.http.HttpClient;

import java.util.Scanner;

/**
 * ClassName: HashTableDemo
 * Package: com.dhnsoft.数据结构与算法.哈希表
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/12 10:25
 * @Version 1.0
 */
public class HashTableDemo {

    public static void main(String[] args) {
        HashTable hashTab = new HashTable(7);

        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("add:  添加雇员");
            System.out.println("list: 显示雇员");
            System.out.println("exit: 退出系统");
            System.out.println("find: 查找雇员");
            System.out.println("del:  删除雇员");

            key = scanner.next();
            switch (key){
                case "add" :
                    System.out.println("输入id:");
                    int id = scanner.nextInt();
                    System.out.println("输入名字:");
                    String name = scanner.next();
                    Emp emp = new Emp(id,name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入要查找的id");
                    id = scanner.nextInt();
                    hashTab.findEmpById(id);
                    break;
                case "del":
                    System.out.println("请输入要删除的id");
                    id = scanner.nextInt();
                    hashTab.delById(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
            }
        }
    }



}

class Emp{
    int id;
    String name;
    Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

}

class EmpLinkedList{
    private Emp head = new Emp(-1,"");

    //添加雇员到链表
    // 1.假定，当添加雇员时，id是自增长的，即id的分配总是从小到大
    // 因此我们将该雇员直接加入到本链表的最后
     public void add(Emp emp){
         if(head==null){
             head.next = emp;
             return;
         }
         Emp temp = head;
         while (temp.next!=null){
             temp = temp.next;
         }
         temp.next = emp;
     }

    //根据id删除雇员
    public void delById(int id,int no){
        if(head==null){
            System.out.printf("第%d条链表为空",no+1);
            return;
        }
        Emp emp = head;
        boolean flag = false;
        while (emp.next!=null){
            if(emp.next.id==id){
                flag = true;
                break;
            }
            emp = emp.next;
        }
        if(flag==true){
            emp.next = emp.next.next;
        }
    }

     public void list(int no){
         if(head==null){
             System.out.printf("第%d条链表为空",no+1);
             return;
         }
         System.out.printf("第%d条链表为空",no+1);
         Emp curEmp = head.next;
         while (curEmp!=null){
             System.out.printf("=> id=%d name=%s\t",curEmp.id,curEmp.name);
             curEmp = curEmp.next;
         }
         System.out.println();
     }

     public Emp findEmpById(int no){
         if(head==null){
             System.out.println("链表为空");
             return null;
         }
         Emp cur = head.next;
         boolean flag =false;
         while (cur!=null){
             if(cur.id==no){
                 flag =true;
                 break;
             }
             cur = cur.next;
         }
         if(flag!=false){
             return cur;
         }
         return null;
     }
}

class HashTable{
    private EmpLinkedList[] empLinkedListArray;
    private int size;
    public HashTable(int size){
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];
        //分别初始化每一个链表
        for (int i=0;i<size;i++){
            empLinkedListArray[i] = new EmpLinkedList();
        }

    }

    //添加emp到EmpLinkedList数组中
    public void add(Emp emp){
        int empLinkedListNo = hashFun(emp.id);
        empLinkedListArray[empLinkedListNo].add(emp);
    }

    //遍历数组
    public void list(){
        for (int i=0;i<size;i++){
            empLinkedListArray[i].list(i);
        }
    }

    //根据输入id查找雇员
    public void findEmpById(int id){
        for (int i=0;i<size;i++){
            Emp empById = empLinkedListArray[i].findEmpById(id);
            if(empById!=null){//找到
                System.out.printf("在第%d条链表中找到雇员id=%d\n",i+1,id);
            }
            else{
                System.out.println("在哈希表中，没有找到该雇员");
            }
        }
    }

    public void delById(int id){
        for (int i=0;i<size;i++){
            empLinkedListArray[i].delById(id,i);
        }
    }

    public int hashFun(int id){
        return id%size;
    }




}